﻿using Ledict.Core.Data;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ledict.ConditionExtract.DAL
{
    /// <summary>
    /// 获取卷宗类型
    /// </summary>
   public class SYS_CaseType_DocTypeDAL
    {
        private string DbName;
        public SYS_CaseType_DocTypeDAL()
        {
            DbName = "JudgeDoc";
        }

        /// <summary>
        /// 根据案件类型获取卷宗类型
        /// </summary>
        /// <returns></returns>
        public DataTable GetSysDocTypeDataByCaseTypeID(string caseTypeCode)
        {
            string sql = @" select CD_Name as Name,CD_Code as ID from SYS_CaseType_DocType where CD_Code in(
select CD_Code from SYS_CaseTypeDocRel where CT_Code = '" + caseTypeCode + "' and CDR_IsAnalysableDoc=1  )and CD_DocType = 1 and CD_Remark<>'废弃' ";
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB != null && OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }

        /// <summary>
        /// 根据案件类型获取文书类型
        /// </summary>
        /// <returns></returns>
        public DataTable GetSysDocTypeDataByCaseTypeIDNew(string caseTypeCode)
        {
            string sql = @" select CD_Name as Name,CD_ID as ID from SYS_CaseType_DocType where CD_Code in(
select CD_Code from SYS_CaseTypeDocRel where CT_Code = '" + caseTypeCode + "')and CD_DocType = 1  ";
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB != null && OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }

        /// <summary>
        /// 获取证明配置信息
        /// </summary>
        /// <param name="ccId"></param>
        /// <returns></returns>
        public DataTable GetSysCaseType(string ccId)
        {
            string sql =string.Format( @" SELECT b.CD_ID,a.CD_Name FROM SYS_CaseType_DocType a
INNER JOIN SYS_CaseCauseDocRel b ON a.CD_ID = b.CD_ID
WHERE b.CC_ID = '{0}' and b.CCD_type = '1'  ",ccId);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB != null && OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }

        /// <summary>
        /// 获取文书类型
        /// </summary>
        /// <returns></returns>
        public DataTable GetSysCaseTypeDocType()
        {
            string sql = @" select CD_ID as ID, CD_Name as Name,CD_Code as Code,CD_ParentID from SYS_CaseType_DocType ";
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB != null && OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }
    }
}
